﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BLL;
using HighLowGameBLL;

/// <summary>
/// Summary description for WSContent
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WSContent : System.Web.Services.WebService
{

    public WSContent()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod(EnableSession = true)]
    public int HelpUpdate(int id, int gameId, int type, string title, string description, string content, int status)
    {
        var ctr = new CtrHiLoHelp();
        int? result = 0;
        string des = "";
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);

        if (id > 0)
        {
            if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
                return -1;
            result = ctr.UpdateHiLoHelp(id, gameId, title, content, description, type, status);
            des = "Cập nhật " + (type == 1 ? "Điều khoản" : "Trợ giúp") + " có mã [HelpId: " + id + "] với các thông tin: [title: " + title + ", type: " + type + ", status: " + status + "]";
        }
        else
        {
            if ((p & (int)Config.EnumPermission.New) != (int)Config.EnumPermission.New)
                return -1;
            ctr.InsertHiLoHelp(gameId, title, content, description, type, status, ref result);
            des = "Thêm mới " + (type == 1 ? "Điều khoản" : "Trợ giúp") + " có mã [HelpId: " + result + "] với các thông tin: [title: " + title + ", type: " + type + ", status: " + status + "]";
        }

        if (result > 0)
        {
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result ?? 0;
    }
    [WebMethod(EnableSession = true)]
    public int HelpUpdateStatus(int id, int status)
    {
        var ctr = new CtrHiLoHelp();
        int? result = 0;
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Edit) != (int)Config.EnumPermission.Edit)
            return -1;
        result = ctr.UpdateHiLoHelp(id, status);
        
        if (result > 0)
        {
            var des = "Cập nhật trạng thái trợ giúp " + " có mã [HelpId: " + id + "] với status: " + status + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result ?? 0;
    }
    [WebMethod(EnableSession = true)]
    public int HelpDelete(int id)
    {
        var ctr = new CtrHiLoHelp();
        int? result = 0;
        string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
        int p = CtrAdmin.GetPermission(function);
        if ((p & (int)Config.EnumPermission.Delete) != (int)Config.EnumPermission.Delete)
            return -1;
        result = ctr.DeleteHiLoHelp(id);
        
        if (result > 0)
        {
            var des = "Xóa trợ giúp " + " có mã [HelpId: " + id + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result ?? 0;
    }
    
}
